<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item v-if="dataForm.userId == 0" label="密码" prop="password">
      <el-input type="password" v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="启用状态" prop="status">
      <el-select v-model="dataForm.status" filterable placeholder="启用状态" clearable>
        <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="真实姓名" prop="realName">
      <el-input v-model="dataForm.realName" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-select v-model="dataForm.gender" filterable placeholder="性别" clearable>
        <el-option
            v-for="item in genderOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="职务" prop="post">
      <el-input v-model="dataForm.post" placeholder="职务"></el-input>
    </el-form-item>
    <el-form-item label="照片" prop="photo">
      <el-upload
        drag
        :action="url"
        :before-upload="beforeUploadHandle"
        :on-success="successHandle"
        :file-list="fileList"
        style="text-align: left;">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只支持jpg、png、gif格式的图片!</div>
      </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        genderOptions: [
          {
            label: "男",
            value: "0"
          },
          {
            label: "女",
            value: "1"
          }
        ],
        statusOptions: [
          {
            label: "禁用",
            value: 0
          },
          {
            label: "正常",
            value: 1
          }
        ],
        url: '',
        num: 0,
        successNum: 0,
        fileList: [],
        dataForm: {
          userId: 0,
          userName: '',
          mobile: '',
          password: '',
          email: '',
          status: '',
          realName: '',
          gender: '',
          post: '',
          photo: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '启用状态不能为空', trigger: 'blur' }
          ],
          realName: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          post: [
            { required: true, message: '职务不能为空', trigger: 'blur' }
          ],
          photo: [
            { required: true, message: '照片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('Authorization')}`)
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/app/appuser/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.data.userName
                this.dataForm.mobile = data.data.mobile
                this.dataForm.password = data.data.password
                this.dataForm.email = data.data.email
                this.dataForm.status = data.data.status
                this.dataForm.realName = data.data.realName
                this.dataForm.gender = data.data.gender
                this.dataForm.post = data.data.post
                this.dataForm.photo = data.data.photo
              }
            })
          }
        })
      },
      // 上传之前
      beforeUploadHandle (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        // this.dataForm.materialType = file.type
        this.num++
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.fileList = []
        this.successNum++
        if (response && response.code === 0) {
          this.dataForm.photo = response.url
          if (this.num === this.successNum) {
            this.$confirm('文件上传成功', '提示', {
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(() => {
              this.visible = false
            })
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/appuser/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'mobile': this.dataForm.mobile,
                'password': this.dataForm.password,
                'email': this.dataForm.email,
                'status': this.dataForm.status,
                'realName': this.dataForm.realName,
                'gender': this.dataForm.gender,
                'post': this.dataForm.post,
                'photo': this.dataForm.photo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>